/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.gui;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import model.dao.DaoAtividade;
import model.dominio.Atividade;

/**
 *
 * @author Teao
 */
public class GuiAtivAdicionar extends javax.swing.JPanel {

    /**
     * Creates new form GuiAdmCadastrar
     */
    public GuiAtivAdicionar() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nome = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tipo = new javax.swing.JTextField();
        valor = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        comboTurma = new javax.swing.JComboBox();
        data = new javax.swing.JFormattedTextField();

        setLayout(null);

        jLabel1.setText("Nome:");
        add(jLabel1);
        jLabel1.setBounds(20, 30, 50, 17);

        nome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomeActionPerformed(evt);
            }
        });
        add(nome);
        nome.setBounds(60, 20, 380, 30);

        jButton2.setText("Salvar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2);
        jButton2.setBounds(160, 240, 160, 50);

        jLabel3.setText("Tipo:");
        add(jLabel3);
        jLabel3.setBounds(20, 70, 40, 17);

        jLabel4.setText("Valor:");
        add(jLabel4);
        jLabel4.setBounds(280, 110, 40, 17);

        jLabel5.setText("Data:");
        add(jLabel5);
        jLabel5.setBounds(20, 110, 50, 17);

        tipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoActionPerformed(evt);
            }
        });
        add(tipo);
        tipo.setBounds(60, 60, 380, 30);
        add(valor);
        valor.setBounds(330, 100, 110, 30);

        jLabel2.setText("Turma:");
        add(jLabel2);
        jLabel2.setBounds(10, 140, 50, 30);

        add(comboTurma);
        comboTurma.setBounds(60, 150, 380, 30);

        try {
            data.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        data.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dataActionPerformed(evt);
            }
        });
        add(data);
        data.setBounds(60, 100, 150, 30);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        Atividade ativ = new Atividade();
        if (nome.getText().isEmpty() || tipo.getText().isEmpty()|| data.getText().equals("  /  /    ") ){
            JOptionPane.showMessageDialog(this,"Digite os dados corretamente","ERRO", JOptionPane.ERROR_MESSAGE);
        }else{
            Date date = new Date();
            DateFormat formatter = new SimpleDateFormat("dd-mm-YYYY");
            try {
                date= formatter.parse(data.getText());
            } catch (ParseException ex) {
                JOptionPane.showMessageDialog(null, "Formato errado");
            }
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            ativ.setNome(nome.getText());
            ativ.setTipo(tipo.getText());
            ativ.setData(c);
            DaoAtividade dao = new DaoAtividade();
            ativ = (Atividade) dao.salvar(ativ);
            if(ativ != null){
                JOptionPane.showMessageDialog(this,"Dados registrados com sucesso","Sucesso", JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(false);
            }
            }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void nomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nomeActionPerformed

    private void tipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tipoActionPerformed

    private void dataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dataActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dataActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboTurma;
    private javax.swing.JFormattedTextField data;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField nome;
    private javax.swing.JTextField tipo;
    private javax.swing.JTextField valor;
    // End of variables declaration//GEN-END:variables
}
